<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('mdl_reff_unit');
		$this->load->model('mdl_reff_petunjuk');
		$this->load->model('mdl_t_disposisi');
		$this->load->model('mdl_reff_status');
		$this->load->model('mdl_status_disposisi');
		$this->load->model('mdl_t_status_surat_masuk');
		$this->load->model('mdl_t_surat_masuk');
	}



	public function index() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}

		$a['page']	= "d_amain";

		$this->load->view('admin/aaa', $a);


	}

	public function klas_surat() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}


		/* pagination */
		$total_row		= $this->db->query("SELECT * FROM ref_klasifikasi")->num_rows();
		$per_page		= 10;

		$awal	= $this->uri->segment(4);
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;

		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;

		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/klas_surat/p");

		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);

		$cari					= addslashes($this->input->post('q'));

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$nama					= addslashes($this->input->post('nama'));
		$uraian					= addslashes($this->input->post('uraian'));

		$cari					= addslashes($this->input->post('q'));


		if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM ref_klasifikasi WHERE nama LIKE '%$cari%' OR uraian LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "l_klas_surat";
		} else if ($mau_ke == "add") {
			$a['page']		= "f_klas_surat";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM ref_klasifikasi WHERE id = '$idu'")->row();
			$a['page']		= "f_klas_surat";
		} else if ($mau_ke == "act_edt") {
			$this->db->query("UPDATE ref_klasifikasi SET nama = '$nama', uraian = '$uraian' WHERE id = '$idp'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
			redirect('admin/klas_surat');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM ref_klasifikasi LIMIT $awal, $akhir ")->result();
			$a['page']		= "l_klas_surat";
		}

		$this->load->view('admin/aaa', $a);
	}

	public function surat_masuk() {
		//var_dump($this->session->userdata);exit;
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}

		/* pagination */

		$per_page		= 10;

		$awal	= $this->uri->segment(4);
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;

		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;



		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);

		$cari					= addslashes($this->input->post('q'));

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$no_agenda				= addslashes($this->input->post('no_agenda'));
		$indek_berkas			= addslashes($this->input->post('indek_berkas'));
		$kode					= addslashes($this->input->post('kode'));
		$dari					= addslashes($this->input->post('dari'));
		$no_surat				= addslashes($this->input->post('no_surat'));
		$tgl_surat				= addslashes($this->input->post('tgl_surat'));
		$uraian					= addslashes($this->input->post('uraian'));
		$ket					= addslashes($this->input->post('ket'));
		$status					= addslashes($this->input->post('status'));
		$id_disposisi			= addslashes($this->input->post('id_disposisi'));
		$kpd_yth				= addslashes($this->input->post('kpd_yth'));

		$cari					= addslashes($this->input->post('q'));

		//upload config
		$config['upload_path'] 		= './upload/surat_masuk';
		$config['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
		$config['max_size']			= '5000';
		$config['max_width']  		= '3000';
		$config['max_height'] 		= '3000';
		$this->load->library('upload', $config);

		if ($mau_ke == "del") {
			$this->db->query("DELETE FROM t_surat_masuk WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('admin/surat_masuk');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM t_surat_masuk WHERE isi_ringkas LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "l_surat_masuk";
		} else if ($mau_ke == "add") {
			$a['page']		= "f_surat_masuk";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM t_surat_masuk WHERE id = '$idu'")->row();
			/*$decrypt_id=$this->encryption->safe_b64decode($idu);
			$arr_decrypt=explode('-', $decrypt_id);
			$get_id_surat = $arr_decrypt[0];
			$get_id_disposisi = $arr_decrypt[1];
			$get_kd_unit=$this->session->userdata('kd_unit');

			$a['datpil']	= $this->db->query("SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.kd_unit,c.id_disposisi FROM t_surat_masuk a
					INNER JOIN t_disposisi b ON a.id = b.id_surat
					INNER JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					WHERE a.id = '" . $get_id_surat . "' AND c.id_disposisi='" . $get_id_disposisi . "' AND c.kd_unit='" . $get_kd_unit . "' ")->row();
*/

			$a['page']		= "f_surat_masuk";
		}else if ($mau_ke == "sts") {

			$decrypt_id=$this->encryption->safe_b64decode($idu);
			$arr_decrypt=explode('-', $decrypt_id);
			$get_id_surat = $arr_decrypt[0];
			$get_id_disposisi = $arr_decrypt[1];

			$get_kd_unit=$this->session->userdata('kd_unit');
			if ($get_id_disposisi=='0'){

				$a['datpil']	= $this->db->query("SELECT * FROM t_surat_masuk a
					WHERE a.id = '" . $get_id_surat . "'")->row();
			}else{

				$a['datpil']	= $this->db->query("SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.kd_unit,c.id_disposisi FROM t_surat_masuk a
					INNER JOIN t_disposisi b ON a.id = b.id_surat
					INNER JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					WHERE a.id = '" . $get_id_surat . "' AND c.id_disposisi='" . $get_id_disposisi . "' AND c.kd_unit='" . $get_kd_unit . "' ")->row();

			}






			$a['page']		= "f_surat_masuk_status";
		}else if ($mau_ke == "act_add") {
			$data['kode'] = $kode;
				$data['no_agenda'] = $no_agenda;
				$data['indek_berkas'] = $indek_berkas;
				$data['uraian'] = $uraian;
				$data['dari'] = $dari;
				$data['no_surat'] = $no_surat;
				$data['tgl_surat'] = $tgl_surat;
				$data['tgl_diterima'] =  NOW();
				$data['kpd_yth'] = $kpd_yth;
				$data['ket'] = $ket;
				$data['keterangan'] = $ket;

				$data['pengolah'] = '1';
				$data['is_surat_masuk'] = '1';
				$data['userid'] = $this->session->userdata('admin_id');
			if ($this->upload->do_upload('file_surat')) {

				$up_data	 	= $this->upload->data();
				$data['file'] = $up_data['file_name'];
				//$this->db->query("INSERT INTO t_surat_masuk VALUES (NULL, '$kode', '	$no_agenda', '$indek_berkas', '$uraian', '$dari', '$no_surat','$tgl_surat', NOW(),'$kpd_yth', '$ket', '".$up_data['file_name']."', '".$this->session->userdata('admin_id')."')");
			} else {
				$data['file'] = '';
				//$this->db->query("INSERT INTO t_surat_masuk VALUES (NULL, '$kode', '$no_agenda', '$indek_berkas', '$uraian', '$dari', '$no_surat', '$tgl_surat', NOW(),'$kpd_yth', '$ket', '', '".$this->session->userdata('admin_id')."')");
			}
			$this->mdl_t_surat_masuk->insert($data);
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added. ".$this->upload->display_errors()."</div>");
			redirect('admin/surat_masuk');
		} else if ($mau_ke == "act_edt") {
			if ($this->upload->do_upload('file_surat')) {
				$up_data	 	= $this->upload->data();

				$this->db->query("UPDATE t_surat_masuk SET kode = '$kode', no_agenda = '$no_agenda', indek_berkas = '$indek_berkas', isi_ringkas = '$uraian', dari = '$dari', no_surat = '$no_surat', tgl_surat = '$tgl_surat', kpd_yth='$kpd_yth', keterangan = '$ket', file = '".$up_data['file_name']."' WHERE id = '$idp'");
			} else {
				$this->db->query("UPDATE t_surat_masuk SET kode = '$kode', no_agenda = '$no_agenda', indek_berkas = '$indek_berkas', isi_ringkas = '$uraian', dari = '$dari', no_surat = '$no_surat', tgl_surat = '$tgl_surat', kpd_yth='$kpd_yth', keterangan = '$ket' WHERE id = '$idp'");
			}

			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated. ".$this->upload->display_errors()."</div>");
			redirect('admin/surat_masuk');
		}else if ($mau_ke == "act_sts") {


			//upload config
			$config_sts['upload_path'] 		= './upload/surat_masuk_status';
			$config_sts['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
			$config_sts['max_size']			= '5000';
			$config_sts['max_width']  		= '3000';
			$config_sts['max_height'] 		= '3000';
			$this->load->library('upload', $config_sts);

			/*
			if ($this->upload->do_upload('file_Status')) {
				$up_data_sts	 	= $this->upload->data();

				$this->db->query("INSERT INTO t_status_surat_masuk VALUES (NULL, '$status', '$ket', '".$up_data_sts['file_name']."')");
			} else {
				$this->db->query("INSERT INTO t_status_surat_masuk VALUES (NULL, '$status', '$ket', '')");
			}*/

			$datas['status'] = $status;
			$datas['keterangan'] = $ket;

			$datas['id_surat'] = $idp;
			$datas['kd_unit'] = $this->session->userdata('kd_unit');

			if ($this->upload->do_upload('file_Status')) {
				$up_data_sts	 	= $this->upload->data();
				$datas['file'] 		= $up_data_sts['file_name'];
			} else {
				$datas['file'] = '';
			}
			if ($id_disposisi!='0'){
				$datas['id_disposisi'] = $id_disposisi;

				$this->mdl_status_disposisi->update($datas);
			}else{
				$datas['level']=$this->session->userdata('admin_level');
				$datas['userid']=$this->session->userdata('admin_id');
				$this->mdl_t_status_surat_masuk->insert($datas);
				$datas_sm['status'] = $status;
				$datas_sm['id_surat'] = $idp;
				$this->mdl_t_surat_masuk->update($datas_sm);
			}


			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been Update. ".$this->upload->display_errors()."</div>");
			redirect('admin/surat_masuk');
		}else {
			$var_kd_unit=$this->session->userdata('kd_unit');
			$a['kd_unit'] = $var_kd_unit;
			//$a['data']	= $this->db->query("SELECT * FROM t_surat_masuk LIMIT $awal, $akhir ")->result();
			if (($this->session->userdata('admin_level') == "Admin") || ($this->session->userdata('admin_level') == "TU") ){
				/*$a['data']	= $this->db->query("SELECT * FROM t_surat_masuk LIMIT $awal, $akhir ")->result();*/

			/*	$total_row		= $this->db->query("SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.kd_unit,c.id_disposisi,c.status FROM t_surat_masuk a
					LEFT JOIN t_disposisi b ON a.id = b.id_surat
					LEFT JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					ORDER BY a.id")->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");

				$a['data']		= $this->db->query("SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.kd_unit,c.id_disposisi,c.status FROM t_surat_masuk a
					LEFT JOIN t_disposisi b ON a.id = b.id_surat
					LEFT JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					ORDER BY a.id
					LIMIT $awal, $akhir ")->result();*/




				/*$total_row		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status from
									(SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status FROM t_surat_masuk a
									LEFT JOIN t_disposisi b ON a.id = b.id_surat
									UNION ALL
									SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status FROM t_status_disposisi c
									INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
									) tbl
									order by id,is_surat_masuk desc")->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");

				$a['data']		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status from
								(SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status FROM t_surat_masuk a
								LEFT JOIN t_disposisi b ON a.id = b.id_surat
								UNION ALL
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
								) tbl
								order by id,is_surat_masuk desc
								LIMIT $awal, $akhir ")->result();*/

				$total_row	= $this->mdl_t_surat_masuk->get_union_table_all_row($awal, $akhir,'', false)->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");
				$a['data']  = $this->mdl_t_surat_masuk->get_union_table_all_row($awal, $akhir,'', true)->result();

			}elseif(($this->session->userdata('admin_level') == "Direktorat")){
				$get_sub_kd_unit = substr($var_kd_unit, 0, 2);
				$total_row		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'','' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
								WHERE kd_unit LIKE '" .$get_sub_kd_unit ."%'

					")->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");

				$a['data']		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'','' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
								WHERE kd_unit LIKE '" .$get_sub_kd_unit ."%' order by id,is_surat_masuk desc LIMIT $awal, $akhir ")->result();


			}elseif(($this->session->userdata('admin_level') == "Subdit") || ($this->session->userdata('admin_level') == "Sie")){
				$get_sub_kd_unit = substr($var_kd_unit, 0, 4);

				/*$a['data']		= $this->db->query("SELECT * FROM t_surat_masuk
					LEFT JOIN t_status_disposisi ON t_surat_masuk.id = t_status_disposisi.id_surat
					WHERE t_status_disposisi.kd_unit LIKE '" . $get_sub_kd_unit . "%'
					LIMIT $awal, $akhir ")->result();*/

				$total_row		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'','' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
					WHERE kd_unit LIKE '" .$get_sub_kd_unit ."%'")->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");

				$a['data']		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'','' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
					WHERE kd_unit LIKE '" .$get_sub_kd_unit ."%' order by id,is_surat_masuk desc LIMIT $awal, $akhir ")->result();

			}elseif($this->session->userdata('admin_level') == "Staff"){
				$total_row		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,userid,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.userid,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'',a.userid,'' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
					WHERE userid like '" . $this->session->userdata('admin_id') . "'  ")->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");

					$a['data']		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,userid,id_status_disposisi from
								(
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.kd_unit,c.userid,c.id as id_status_disposisi FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
                                UNION ALL
								SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'',a.userid,'' FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
                                ) tbl
					WHERE userid like '" . $this->session->userdata('admin_id') . "'  order by id,is_surat_masuk desc
					LIMIT $awal, $akhir ")->result();
			}else{
				$total_row		= $this->db->query("SELECT id,id_disposisi,is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,userid,id_status_disposisi from
								(SELECT a.id,b.id_disposisi,a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,a.userid,c.id as id_status_disposisi FROM t_surat_masuk a
								LEFT JOIN t_disposisi b ON a.id = b.id_surat
								UNION ALL
								SELECT c.id_surat as id,c.id_disposisi,c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,c.status,c.userid,'' FROM t_status_disposisi c
								INNER JOIN t_surat_masuk a on (c.id_surat=a.id)
								) tbl
								WHERE userid like '" . $this->session->userdata('admin_id') . "'")->num_rows();


				$arr_where = "WHERE userid like '" . $this->session->userdata('admin_id') . "'";
				$total_row	= $this->mdl_t_surat_masuk->get_union_table_all_row($awal, $akhir,$arr_where, false)->num_rows();
				$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_masuk/p");
				$a['data']  = $this->mdl_t_surat_masuk->get_union_table_all_row($awal, $akhir,$arr_where, true)->result();

			}
				//echo $this->db->last_query();exit;
				$a['page']		= "l_surat_masuk";
			//echo 'kd_unit:' . $var_kd_unit . '-' . $get_sub_kd_unit;
		}

		$this->load->view('admin/aaa', $a);

	}



	public function surat_disposisi() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}


		//ambil variabel URL
		$mau_ke					= $this->uri->segment(4);
		$idu1					= $this->uri->segment(3);
		$idu2					= $this->uri->segment(5);
		$a['idu1'] = $idu1;
		$a['idu2'] = $idu2;

		$cari					= addslashes($this->input->post('q'));
		$petunjuk = array();
		$kpd_yth = array();
		$arr_kpd = '';
		$arr_p = '';
		$var_substring_val = '';



		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$id_surat				= addslashes($this->input->post('id_surat'));
		$kpd_yth				= $this->input->post('kpd_yth');
		$isi_disposisi			= addslashes($this->input->post('isi_disposisi'));
		$sifat					= addslashes($this->input->post('sifat'));
		$batas_waktu			= addslashes($this->input->post('batas_waktu'));
		$catatan				= addslashes($this->input->post('catatan'));
		$petunjuk				= $this->input->post('petunjuk');
		$cari					= addslashes($this->input->post('q'));

		$ket					= addslashes($this->input->post('ket'));
		$status					= addslashes($this->input->post('status'));
		$id_disposisi			= addslashes($this->input->post('id_disposisi'));

		for($x=0;$x<count($petunjuk);$x++){
			$arr_p .=  $petunjuk[$x] . ';';
		}


		/* pagination */
		$total_row		= $this->db->query("SELECT * FROM t_disposisi WHERE id_surat = '$idu1'")->num_rows();
		$per_page		= 10;

		$awal	= $this->uri->segment(4);
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;

		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;

		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/surat_disposisi/".$idu1."/p");

		$a['judul_surat']	= gval("t_surat_masuk", "id", "isi_ringkas", $idu1);
		$level_user=$this->session->userdata('admin_level');

		$kd_unit=$this->session->userdata('kd_unit');
		$get_kd_unit=$this->session->userdata('kd_unit');

		if (($this->session->userdata('admin_level') == "Admin") || ($this->session->userdata('admin_level') == "TU") ){
				$query_kd_unit = '';
		}else{
			$query_kd_unit = 'AND c.kd_unit=' . $get_kd_unit;
		}

		$var_escape_self_id = substr($kd_unit, -2, 2);
		if ($level_user=='Subdit'){
			$var_substring_val = 4;
			$substring_kd_unit = substr($kd_unit, 0,$var_substring_val);
			//$a['reff_unit'] = $this->mdl_reff_unit->get_by_like($substring_kd_unit,$var_escape_self_id);
			$a['reff_unit'] = $this->mdl_reff_unit->get_t_admin($substring_kd_unit,$var_escape_self_id);

		}elseif ($level_user=='Sie'){
			$var_escape_self_id = substr($kd_unit, -2, 2);

			$var_substring_val = 4;
			$substring_kd_unit = substr($kd_unit, 0,$var_substring_val);
			//$a['reff_unit'] = $this->mdl_reff_unit->get_by_like($substring_kd_unit,$var_escape_self_id);
			$a['reff_unit'] = $this->mdl_reff_unit->get_t_admin_for_staff($substring_kd_unit,$var_escape_self_id);

		}elseif($level_user=='Direktorat'){
			$var_substring_val = 2;
			$substring_kd_unit = substr($kd_unit, 0,$var_substring_val);
			$a['reff_unit'] = $this->mdl_reff_unit->get_by_like($substring_kd_unit,$var_escape_self_id);
		}elseif(($level_user=='Admin') || ($level_user=='TU')){
			$var_substring_val = 2;
			$substring_kd_unit = substr($kd_unit, 0,$var_substring_val);
			$a['reff_unit'] = $this->mdl_reff_unit->get_by_id($substring_kd_unit);
		}

		$a['level_user'] = $level_user;
		//echo 'kode_unit:' . $substring_kd_unit;

		$a['reff_petunjuk'] = $this->mdl_reff_petunjuk->get_all();

		if ($mau_ke == "del") {
			$this->db->query("DELETE FROM t_disposisi WHERE id = '$idu2'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('admin/surat_disposisi/'.$idu2);
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM t_disposisi WHERE isi_disposisi LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "l_surat_disposisi";
		} else if ($mau_ke == "add") {

			$a['page']		= "f_surat_disposisi";
		} else if ($mau_ke == "edt") {
			/*$a['datpil']	= $this->db->query("SELECT * FROM t_disposisi WHERE id = '$idu2'")->row();*/
			$decrypt_id=$this->encryption->safe_b64decode($idu2);
			$arr_decrypt=explode('-', $decrypt_id);
			$get_id_surat = $arr_decrypt[0];
			$get_id_disposisi = $arr_decrypt[1];


			$str_query  = "SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,b.catatan,b.batas_waktu, b.petunjuk,b.sifat,b.isi_disposisi,
					c.kd_unit,c.id_disposisi
					FROM t_surat_masuk a
					INNER JOIN t_disposisi b ON a.id = b.id_surat
					INNER JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					WHERE a.id = '" . $get_id_surat . "' AND c.id_disposisi='" . $get_id_disposisi . "'";
			$a['datpil']	= $this->db->query($str_query)->row();

			$a['get_status_disposisi']	= $this->mdl_status_disposisi->get_by_id_disposisi($get_id_disposisi);
			$a['page']		= "f_surat_disposisi";
		} else if ($mau_ke == "sls") {
			$q_sls = "SELECT * FROM t_disposisi WHERE id_surat = '$idu2'";
			echo $idu2;
			$a['datpil']	= $this->db->query($q_sls)->row();
			$a['page']		= "f_disposisi_status";
		}
		else if ($mau_ke == "act_add") {
			//$this->db->query("INSERT INTO t_disposisi VALUES (NULL, '$id_surat', '$kpd_yth', '$isi_disposisi', '$sifat', '$batas_waktu', '$catatan','".$this->session->userdata('kd_unit')."')");
			$data_disposisi['id_surat'] = $idp;
			$data_disposisi['kpd_yth'] = '';
			$data_disposisi['isi_disposisi'] = $isi_disposisi;
			$data_disposisi['sifat'] = $sifat;
			$data_disposisi['batas_waktu'] = $batas_waktu;
			$data_disposisi['catatan'] = $catatan;
			$data_disposisi['kd_unit'] = $this->session->userdata('kd_unit');
			$data_disposisi['petunjuk'] = $arr_p;
			$data_disposisi['id_disposisi'] = 'idd' . date('Yhismd');

			$data_status_disposisi['id_surat'] = $idp;
			$data_status_disposisi['id_disposisi'] = 'idd' . date('Yhismd');
			$data_status_disposisi['level'] = $this->session->userdata('admin_level');

			$query_ins=$this->mdl_t_disposisi->insert($data_disposisi);


			for($j=0;$j<count($kpd_yth);$j++){
				//$arr_kpd .=  $kpd_yth[$j] . ';';
				if (($level_user=='Subdit') || ($level_user=='Sie')){
					$data_status_disposisi['userid'] = $kpd_yth[$j];

					$get_kd_unit = $this->mdl_reff_unit->get_t_admin_by_id($kpd_yth[$j])->row();
					 $data_status_disposisi['kd_unit'] = $get_kd_unit->kd_unit;
				}else{
					$data_status_disposisi['userid'] = '';
					$data_status_disposisi['kd_unit'] = $kpd_yth[$j];
				}

				$q_ins_disposisi=$this->mdl_status_disposisi->insert($data_status_disposisi);
			}

			if ($query_ins){
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
			}else{
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data failed to save</div>");
			}
			redirect('admin/surat_disposisi/' . $idp);

		} else if ($mau_ke == "act_edt") {
			$del_id_disposisi=$this->mdl_status_disposisi->delete($id_disposisi);
			for($j=0;$j<count($kpd_yth);$j++){
				//$arr_kpd .=  $kpd_yth[$j] . ';';
				if (($level_user=='Subdit') || ($level_user=='Sie')){
					$data_status_disposisi['userid'] = $kpd_yth[$j];

					$get_kd_unit = $this->mdl_reff_unit->get_t_admin_by_id($kpd_yth[$j])->row();
					 $data_status_disposisi['kd_unit'] = $get_kd_unit->kd_unit;
				}else{
					$data_status_disposisi['userid'] = '';
					$data_status_disposisi['kd_unit'] = $kpd_yth[$j];
				}
				$data_status_disposisi['id_disposisi'] = $id_disposisi;
				$data_status_disposisi['id_surat'] = $idp;
				$data_status_disposisi['level'] = $this->session->userdata('admin_level');


				$q_ins_disposisi=$this->mdl_status_disposisi->insert($data_status_disposisi);
			}
			$query_update = "UPDATE t_disposisi SET kpd_yth = '$arr_kpd', isi_disposisi = '$isi_disposisi', sifat = '$sifat', batas_waktu = '$batas_waktu', catatan = '$catatan', petunjuk = '$arr_p' WHERE id_surat = '$idp'";
			$this->db->query($query_update);
			//echo $query_update;
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
			redirect('admin/surat_disposisi/'.$idu1);

		}else if ($mau_ke == "act_sls") {

			//echo '<br/><br/><br/><br/>testt-';

			//upload config
			$config_sts['upload_path'] 		= './upload/surat_disposisi_status';
			$config_sts['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
			$config_sts['max_size']			= '5000';
			$config_sts['max_width']  		= '3000';
			$config_sts['max_height'] 		= '3000';
			$this->load->library('upload', $config_sts);
			if ($this->upload->do_upload('file_Status')) {
				$up_data_sts_disp	 	= $this->upload->data();

				//$this->db->query("INSERT INTO t_status_surat_masuk VALUES (NULL, '$status', '$ket', '".$up_data_sts['file_name']."')");
			} else {
				//$this->db->query("INSERT INTO t_status_surat_masuk VALUES (NULL, '$status', '$ket', '')");
			}


			//$data_where['id_surat'] = $id_surat;

			$update_status_disposisi['id_disposisi'] = $idp;
			$update_status_disposisi['keterangan'] = $ket;
			$update_status_disposisi['status'] = $status;
			$update_status_disposisi['file'] = $up_data_sts_disp['file_name'];

			$update_status_disposisi['id_surat'] = $id_surat;
			$update_status_disposisi['kd_unit'] = $this->session->userdata('kd_unit');
			$q_update_status_disposis = $this->mdl_status_disposisi->update($update_status_disposisi);


			/*$q_update_status_disposis = $this->mdl_status_disposisi->update($update_status_disposisi);
			$this->db->query("UPDATE t_disposisi SET kpd_yth = '$arr_kpd', isi_disposisi = '$isi_disposisi', sifat = '$sifat', batas_waktu = '$batas_waktu', catatan = '$catatan', petunjuk = '$arr_p' WHERE id = '$idp'");*/
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
			redirect('admin/surat_disposisi/'.$idu2);
		}
		 else {
		 /*$decrypt_id=$this->encryption->safe_b64decode($idu2);
		$arr_decrypt=explode('-', $decrypt_id);
		$get_id_surat = $arr_decrypt[0];
		$get_id_disposisi = $arr_decrypt[1];
		$get_kd_unit=$this->session->userdata('kd_unit');*/
		 			/*$str_query  = "SELECT a.id,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,b.catatan,b.batas_waktu, b.petunjuk,
		 			c.kd_unit,c.id_disposisi FROM t_surat_masuk a
					INNER JOIN t_disposisi b ON a.id = b.id_surat
					INNER JOIN t_status_disposisi c ON c.id_disposisi = b.id_disposisi
					WHERE a.id = '" . $get_id_surat . "' AND c.id_disposisi='" . $get_id_disposisi . $query_kd_unit . "'";
			$a['datpil']	= $this->db->query($str_query)->row();*/
			$a['data']		= $this->db->query("SELECT * FROM t_disposisi WHERE id_surat = '$idu1' LIMIT $awal, $akhir ")->result();
			$a['page']		= "l_surat_disposisi";
		}

		$this->load->view('admin/aaa', $a);
	}

	public function pengguna() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}

		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$nama					= addslashes($this->input->post('nama'));
		$alamat					= addslashes($this->input->post('alamat'));
		$kepsek					= addslashes($this->input->post('kepsek'));
		$nip_kepsek				= addslashes($this->input->post('nip_kepsek'));

		$cari					= addslashes($this->input->post('q'));

		//upload config
		$config['upload_path'] 		= './upload';
		$config['allowed_types'] 	= 'gif|jpg|png|pdf|doc|docx';
		$config['max_size']			= '2000';
		$config['max_width']  		= '3000';
		$config['max_height'] 		= '3000';

		$this->load->library('upload', $config);

		if ($mau_ke == "act_edt") {
			if ($this->upload->do_upload('logo')) {
				$up_data	 	= $this->upload->data();

				$this->db->query("UPDATE tr_instansi SET nama = '$nama', alamat = '$alamat', kepsek = '$kepsek', nip_kepsek = '$nip_kepsek', logo = '".$up_data['file_name']."' WHERE id = '$idp'");

			} else {
				$this->db->query("UPDATE tr_instansi SET nama = '$nama', alamat = '$alamat', kepsek = '$kepsek', nip_kepsek = '$nip_kepsek' WHERE id = '$idp'");
			}

			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated</div>");
			redirect('admin/pengguna');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM tr_instansi WHERE id = '1' LIMIT 1")->row();
			$a['page']		= "f_pengguna";
		}

		$this->load->view('admin/aaa', $a);
	}

	public function agenda_surat_masuk() {
		$a['page']	= "f_config_cetak_agenda";
		$this->load->view('admin/aaa', $a);
	}
	public function agenda_surat_keluar() {
		$a['page']	= "f_config_cetak_agenda";
		$this->load->view('admin/aaa', $a);
	}

	public function cetak_agenda() {
		$jenis_surat	= $this->input->post('tipe');
		$tgl_start		= $this->input->post('tgl_start');
		$tgl_end		= $this->input->post('tgl_end');

		$a['tgl_start']	= $tgl_start;
		$a['tgl_end']	= $tgl_end;

		if ($jenis_surat == "agenda_surat_masuk") {
			$a['data']	= $this->db->query("SELECT * FROM t_surat_masuk WHERE tgl_diterima >= '$tgl_start' AND tgl_diterima <= '$tgl_end' ORDER BY id")->result();
			$this->load->view('admin/agenda_surat_masuk', $a);
		} else {
			$a['data']	= $this->db->query("SELECT * FROM t_surat_keluar WHERE tgl_catat >= '$tgl_start' AND tgl_catat <= '$tgl_end' ORDER BY id")->result();
			$this->load->view('admin/agenda_surat_keluar', $a);
		}
	}

	public function manage_admin() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}

		/* pagination */
		$total_row		= $this->db->query("SELECT * FROM t_admin")->num_rows();
		$per_page		= 10;

		$awal	= $this->uri->segment(4);
		$awal	= (empty($awal) || $awal == 1) ? 0 : $awal;

		//if (empty($awal) || $awal == 1) { $awal = 0; } { $awal = $awal; }
		$akhir	= $per_page;

		$a['pagi']	= _page($total_row, $per_page, 4, base_url()."admin/manage_admin/p");

		//ambil variabel URL
		$mau_ke					= $this->uri->segment(3);
		$idu					= $this->uri->segment(4);

		$cari					= addslashes($this->input->post('q'));

		//ambil variabel Postingan
		$idp					= addslashes($this->input->post('idp'));
		$username				= addslashes($this->input->post('username'));
		$password				= md5(addslashes($this->input->post('password')));
		$nama					= addslashes($this->input->post('nama'));
		$nip					= addslashes($this->input->post('nip'));
		$level					= addslashes($this->input->post('level'));

		$cari					= addslashes($this->input->post('q'));


		if ($mau_ke == "del") {
			$this->db->query("DELETE FROM t_admin WHERE id = '$idu'");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been deleted </div>");
			redirect('admin/manage_admin');
		} else if ($mau_ke == "cari") {
			$a['data']		= $this->db->query("SELECT * FROM t_admin WHERE nama LIKE '%$cari%' ORDER BY id DESC")->result();
			$a['page']		= "l_manage_admin";
		} else if ($mau_ke == "add") {
			$a['page']		= "f_manage_admin";
		} else if ($mau_ke == "edt") {
			$a['datpil']	= $this->db->query("SELECT * FROM t_admin WHERE id = '$idu'")->row();
			$a['page']		= "f_manage_admin";
		} else if ($mau_ke == "act_add") {
			$this->db->query("INSERT INTO t_admin VALUES (NULL, '$username', '$password', '$nama', '$nip', '$level')");
			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been added</div>");
			redirect('admin/manage_admin');
		} else if ($mau_ke == "act_edt") {
			if ($password = md5("-")) {
				$this->db->query("UPDATE t_admin SET username = '$username', nama = '$nama', nip = '$nip', level = '$level' WHERE id = '$idp'");
			} else {
				$this->db->query("UPDATE t_admin SET username = '$username', password = '$password', nama = '$nama', nip = '$nip', level = '$level' WHERE id = '$idp'");
			}

			$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data has been updated </div>");
			redirect('admin/manage_admin');
		} else {
			$a['data']		= $this->db->query("SELECT * FROM t_admin LIMIT $awal, $akhir ")->result();
			$a['page']		= "l_manage_admin";
		}

		$this->load->view('admin/aaa', $a);
	}

	public function get_klasifikasi() {
		$kode 				= $this->input->post('kode',TRUE);

		$data 				=  $this->db->query("SELECT id, kode, nama FROM ref_klasifikasi WHERE kode LIKE '%$kode%' ORDER BY id ASC")->result();

		$klasifikasi 		=  array();
        foreach ($data as $d) {
			$json_array				= array();
            $json_array['value']	= $d->kode;
			$json_array['label']	= $d->kode." - ".$d->nama;
			$klasifikasi[] 			= $json_array;
		}

		echo json_encode($klasifikasi);
	}

	public function get_instansi_lain() {
		$kode 				= $this->input->post('dari',TRUE);

		$data 				=  $this->db->query("SELECT dari FROM t_surat_masuk WHERE dari LIKE '%$kode%' GROUP BY dari")->result();

		$klasifikasi 		=  array();
        foreach ($data as $d) {
			$klasifikasi[] 	= $d->dari;
		}

		echo json_encode($klasifikasi);
	}

	public function disposisi_cetak() {
		$idu = $this->uri->segment(3);
		$a['datpil1']	= $this->db->query("SELECT * FROM t_surat_masuk WHERE id = '$idu'")->row();
		$a['datpil2']	= $this->db->query("SELECT * FROM t_disposisi WHERE id = '$idu'")->result();
		$this->load->view('admin/f_disposisi', $a);
	}

	public function passwod() {
		if ($this->session->userdata('admin_valid') == FALSE && $this->session->userdata('admin_id') == "") {
			redirect("admin/login");
		}

		$ke				= $this->uri->segment(3);
		$id_user		= $this->session->userdata('admin_id');

		//var post
		$p1				= md5($this->input->post('p1'));
		$p2				= md5($this->input->post('p2'));
		$p3				= md5($this->input->post('p3'));

		if ($ke == "simpan") {
			$cek_password_lama	= $this->db->query("SELECT password FROM t_admin WHERE id = $id_user")->row();
			//echo

			if ($cek_password_lama->password != $p1) {
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-error">Password Lama tidak sama'.$this->db->last_query().'-'.$cek_password_lama->p.'-'.$p1.'</div>');
				redirect('admin/passwod');
			} else if ($p2 != $p3) {
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-error">Password Baru 1 dan 2 tidak cocok</div>');
				redirect('admin/passwod');
			} else {
				$this->db->query("UPDATE t_admin SET password = '$p3' WHERE id = '1'");
				$this->session->set_flashdata('k_passwod', '<div id="alert" class="alert alert-success">Password berhasil diperbaharui</div>');
				redirect('admin/passwod');
			}
		} else {
			$a['page']	= "f_passwod";
		}

		$this->load->view('admin/aaa', $a);
	}

	//login
	public function login() {
		$this->load->view('admin/login');
	}

	public function do_login() {
		$u 		= $this->security->xss_clean($this->input->post('u'));
		$ta 	= $this->security->xss_clean($this->input->post('ta'));
        $p 		= md5($this->security->xss_clean($this->input->post('p')));

		$q_cek	= $this->db->query("SELECT * FROM t_admin WHERE username = '".$u."' AND password = '".$p."'");
		$j_cek	= $q_cek->num_rows();
		$d_cek	= $q_cek->row();
		//echo $this->db->last_query();

        if($j_cek == 1) {
            $data = array(
                    'admin_id' => $d_cek->id,
                    'admin_user' => $d_cek->username,
                    'admin_nama' => $d_cek->nama,
                    'admin_ta' => $ta,
                    'admin_level' => $d_cek->level,
					'admin_valid' => true,
					'kd_unit' => $d_cek->kd_unit
                    );
			//var_dump($data);exit;
            $this->session->set_userdata($data);
            redirect('admin');
        } else {
			$this->session->set_flashdata("k", "<div id=\"alert\" class=\"alert alert-error\">username or password is not valid</div>");
			redirect('admin/login');
		}
	}

	public function logout(){
        $this->session->sess_destroy();
		redirect('admin/login');
    }

    function detail(){
    	$idsurat=$this->input->post('idsurat');
    	$decrypt_id=$this->encryption->safe_b64decode($idsurat);
		$arr_decrypt=explode('-', $decrypt_id);
		$filter['id_surat'] = $arr_decrypt[0];
		$filter['id_disposisi'] = $arr_decrypt[1];
		$filter['id_status_disposisi'] = $arr_decrypt[2];

		$data['results']=$this->mdl_t_surat_masuk->get_detail($filter);


		$this->load->view('admin/surat_masuk/view_detail', $data);

    }
}




?>